package com.zrrd.jt_admin.user.controller;


import com.zrrd.jt_admin.common.entity.Role;
import com.zrrd.jt_admin.common.entity.UserLevel;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.UserPageVo;
import com.zrrd.jt_admin.user.service.UserLevelService;
import com.zrrd.jt_admin.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-06-30
 */
@Api(tags = "会员等级管理-接口文档")
@RestController
@RequestMapping("/admin/userLevel")
public class UserLevelController {

    @Resource
    private UserLevelService userLevelService;

    @ApiOperation(value = "会员等级列表")
    @GetMapping("/")
    @ResponseBody
    public ResponseResult findByPage(@RequestParam(value = "page")Integer page,
                                     @RequestParam(value = "limit",required = false)Integer limit){
        if(limit==null){
            limit = 10;
        }
        List<UserLevel> userLevels = userLevelService.findByPage(page,limit);
        return ResponseResult.queryResponse(userLevels);
    }

    @ApiOperation(value = "增加会员等级")
    @PostMapping("/")
    @ResponseBody
    public ResponseResult addUserLevel(@RequestBody UserLevel userLevel){
        if (userLevel==null){
            return ResponseResult.addResponse(false);
        }
        boolean save = userLevelService.save(userLevel);
        return ResponseResult.addResponse(save);
    }

    @ApiOperation(value = "修改会员等级")
    @PutMapping("/")
    @ResponseBody
    public ResponseResult updateUserLevel(@RequestBody UserLevel userLevel){
        if (userLevel==null){
            return ResponseResult.updateResponse(false);
        }
        return ResponseResult.updateResponse(userLevelService.updateById(userLevel));
    }

    @ApiOperation(value = "修改会员等级状态")
    @PutMapping("/{id}/updateStatus")
    @ResponseBody
    public ResponseResult updateUserLevelStatus(@PathVariable("id") Integer id,Integer status){
        Boolean bStatus=status==1?true:false;

        boolean flag = userLevelService.lambdaUpdate()
                .eq(UserLevel::getId, id)
                .set(UserLevel::getStatus, bStatus)
                .update();
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "修改会员等级状态")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult deleteUserLevel(@PathVariable("id") Integer id){
        return ResponseResult.removeResponse(userLevelService.removeById(id));
    }

}

